import Vue from 'vue';
import Router from 'vue-router';
import NProgress from 'nprogress'; // 顶部加载进度条
import 'nprogress/nprogress.css'; // 进度条样式
import {getToken} from '@/utils/auth'; // 获取token

import Login from './views/Login.vue';
import Home from './views/home.vue';
// import Register from './views/Register/index';

Vue.use(Router);

import Layout from '@/views/Layout/Layout';

const router = new Router({
    mode: 'history',
    scrollBehavior: () => ({y: 0}),
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/home',
            name: 'Home',
            component: Home,
            meta: {title: '首页', main: false}
        },
        {
            path: '/login',
            name: 'Login',
            component: Login,
            meta: {title: '登录', main: false}
        },
        {
            path: '/',
            component: Layout,
            redirect: 'orderlist',
            meta: {title: '出口订单', main: true, icon: 'chukou'},
            children: [
                {
                    path: 'orderlist',
                    name: 'Orderlist',
                    component: () => import('@/views/Order/index'),
                    meta: {title: ''}
                },
                {
                    path: 'orderdetail',
                    name: 'orderDetail',
                    component: () => import('@/views/Order/orderDetail'),
                    meta: {title: '出口订单详情'}
                }
            ]
        },
        {
            path: '/keyorder',
            component: Layout,
            redirect: '/keyorder/index',
            meta: {title: '重点订单', main: true, icon: 'zhongdianbiaozhu'},
            children: [
                {
                    path: 'index',
                    name: 'keyOrder',
                    component: () => import('@/views/Order/keyOrder'),
                    meta: {title: ''}
                },
                {
                    path: 'orderdetail',
                    name: 'orderDetail',
                    component: () => import('@/views/Order/orderDetail'),
                    meta: {title: '重点订单详情'}
                }
            ]
        },
        {
            path: '/keysetting',
            component: Layout,
            redirect: '/keySetting/index',
            meta: {title: '重点条件', main: true, icon: 'tiaojian'},
            children: [
                {
                    path: 'index',
                    name: 'keySetting',
                    component: () => import('@/views/keySetting/index'),
                    meta: {title: ''}
                },
                {
                    path: 'keycreate',
                    name: 'keyCreate',
                    component: () => import('@/views/keySetting/keyCreate'),
                    meta: {title: '新增关注条件'}
                }
            ]
        },
        {
            path: '/analysis',
            component: Layout,
            redirect: '/analysis/index',
            meta: {title: '数据分析', main: true, icon: 'shuju2'},
            children: [
                {
                    path: 'index',
                    name: 'Analysis',
                    component: () => import('@/views/Analysis/index'),
                    meta: {title: ''}
                },
                {// 出口订单
                    path: 'gatt',
                    name: 'gatt',
                    component: () => import('@/views/Analysis/gatt'),
                    meta: {title: '出口订单'}
                },
                {// 辖区企业
                    path: 'areas',
                    name: 'areas',
                    component: () => import('@/views/Analysis/areas'),
                    meta: {title: '辖区企业'}
                },
                {// 贸易地区
                    path: 'trace',
                    name: 'trace',
                    component: () => import('@/views/Analysis/trace'),
                    meta: {title: '贸易地区'}
                },
                {// 涉美跟踪
                    path: 'trade',
                    name: 'trade',
                    component: () => import('@/views/Analysis/trade'),
                    meta: {title: '涉美跟踪'}
                },

            ]
        },
        {
            path: '/area',
            component: Layout,
            redirect: '/area/index',
            meta: {title: '辖区企业', main: true, icon: '75'},
            children: [
                {
                    path: 'index',
                    name: 'Area',
                    component: () => import('@/views/Area/index'),
                    meta: {title: ''}
                },
                {
                    path: 'companyorder',
                    name: 'companyOrder',
                    component: () => import('@/views/Area/companyOrder'),
                    meta: {title: '企业订单详情'}
                },
                {
                    path: 'export',
                    name: 'export',
                    component: () => import('@/views/Area/export'),
                    meta: {title: '企业导出'}
                }
            ]
        },
        {
            path: '/company',
            component: Layout,
            redirect: '/company/index',
            meta: {title: '企业申报', main: true, icon: 'qiyejianjie'},
            children: [
                {
                    path: 'index',
                    name: 'companyList',
                    component: () => import('@/views/CompanyList/index'),
                    meta: {title: ''}
                }
            ]
        },
        {
            path: '/setting',
            component: Layout,
            redirect: '/setting/index',
            meta: {title: '系统设置', main: true, icon: 'shezhi'},
            children: [
                {
                    path: 'index',
                    name: 'Setting',
                    component: () => import('@/views/Setting/index'),
                    meta: {title: ''}
                }
            ]
        },
        {
            path: '/user',
            component: Layout,
            redirect: '/Area/index',
            meta: {title: '新增辖区企业', main: false, icon: 'user-cog'},
            children: [
                // {
                //   path: 'index',
                //   name: 'index',
                //   component: () => import('@/views/User/index'),
                //   meta: { title: '' }
                // },
                {
                    title: '新增辖区企业',
                    path: 'usercreate',
                    name: 'userCreate',
                    component: () => import('@/views/User/userCreate'),
                    meta: {title: ''}
                }
            ]
        }
    ]
});

// 设置需要排除的路由
let exRouterReg = /\/(register|password|home)/;

router.beforeEach((to, from, next) => {
    NProgress.start();
    if (getToken()) {
        /* 如果token存在 */
        if (to.path === '/login') {
            /* 拦截去login界面的路由到有权限的默认首页 */
            next({path: '/'});
            NProgress.done();
        }
        next();
    } else {
        if (to.path === '/login' || exRouterReg.test(to.path)) {
            next();
        } else {
            next('/login');
        }
    }
});

router.afterEach(() => {
    NProgress.done(); // finish progress bar
});

export default router;
